
var app = getApp()
var util = require('../../utils/util.js')
var Bmob = require('../../utils/bmob.js');
Page({
  data: {
    hideVerifyPhone: true,
    hideBindNewPhone: true,
    oldCode: '',
    smsid:'',
    oldCodeBtnDisabled: false,
    oldCodeStatus: '获取验证码',
    nextStepDisabled: false,
    newPhone: '',
    newCode: '',
    newCodeBtnDisabled: false,
    newCodeStatus: '获取验证码',
    bindNewPhoneBtnDisabled: false,
    codeInterval: 30,
  
  },
  onLoad: function(){
    app.gologin_bomb();
    var userInfo = Bmob.User.current();
   
    if (userInfo.get('mobilePhoneNumber') && userInfo.get('mobilePhoneNumberVerified')){
      this.setData({
        hideVerifyPhone: false
      })
    } else {
      this.setData({
        hideBindNewPhone: false
      })
    }
  },
  sendCodeToOldPhone: function(){
    var that = this;
    var second = that.data.codeInterval,
      interval;
    if (this.data.oldCodeBtnDisabled) {
      return;
    }

    this.setData({
      oldCodeStatus: '正在发送...',
      oldCodeBtnDisabled: true
    })
    Bmob.Sms.requestSmsCode({ "mobilePhoneNumber": app.getUserInfo().get('mobilePhoneNumber') }).then(function (obj){
        
      that.setData({

        smsid: obj.Id,
      })
      interval = setInterval(function () {
        if (second < 0) {
          clearInterval(interval);
          that.setData({
            oldCodeStatus: '获取验证码',
            oldCodeBtnDisabled: false
          })
        } else {
          that.setData({
            oldCodeStatus: second + 's',
          
          })
          second--;
        }
      }, 1000);
          that.setData({

            smsid:obj.Id,
          })
       
      
    },
      
      function (err) {
       
      })

  },
  inputOldCode: function(e){
    this.setData({
      oldCode: e.detail.value
    })
  },
  nextStep_bomb: function(){
    var that = this;
    if(!this.data.oldCode){
      app.showModal({
        content: '请输入验证码'
      })
      return;
    }
    if(this.data.nextStepDisabled){
      return;
    }

    this.setData({
      nextStepDisabled: true
    })
    
    Bmob.Sms.verifySmsCode(that.smsid, oldCode).then(function (obj) {
      that.setData({
        hideVerifyPhone: true,
        hideBindNewPhone: false
      }) //
    }, function (err) {
      that.setData({
        nextStepDisabled: false
      })
    });
        
   
  },
  inputPhone: function(e){
    this.setData({
      newPhone: e.detail.value
    })
  },
  inputNewCode: function(e){
    this.setData({
      newCode: e.detail.value
    })
  },
  sendCodeToNewPhone: function(){
    var that = this,
        newPhone = this.data.newPhone;

    app.getStorage({
      key: 'session_key',
      success: function (res) {
        console.log(res);
        if (res.data == '' ) {
          app.showModal({
            content: '未获取授权，验证码获取失败'
          })
          return;
        };
      }
    })

    if(!util.isPhoneNumber(newPhone)){
      app.showModal({
        content: '请输入正确的手机号码'
      })
      return;
    }
    if(this.data.newCodeBtnDisabled){
      return;
    }

    this.setData({
      newCodeStatus: '正在发送...',
      newCodeBtnDisabled: true
    })
    Bmob.Sms.requestSmsCode({ "mobilePhoneNumber": newPhone }).then(function (obj) {
      var second = that.data.codeInterval,
        interval;
      interval = setInterval(function () {
        if (second < 0) {
          clearInterval(interval);
          that.setData({
            newCodeStatus: '获取验证码',
            newCodeBtnDisabled: false
          })
        } else {
          that.setData({
            newCodeStatus: second + 's',
          })
          second--;
        }
      }, 1000);
  },

  function(err) {
    that.setData({
      oldCodeStatus: '获取验证码',
      oldCodeBtnDisabled: false
    })
  })
  },
  bindNewPhone: function(){
    var that = this,
        newPhone = this.data.newPhone,
        newCode = this.data.newCode;

    if(!newPhone || !newCode){
      return;
    }
    if(!util.isPhoneNumber(newPhone)){
      app.showModal({
        content: '请输入正确的手机号码'
      })
      return;
    }

    if(this.data.bindNewPhoneBtnDisabled){
      return;
    }
    this.setData({
      bindNewPhoneBtnDisabled: true
    })

    var that = this;
    if (this.data.oldCodeBtnDisabled) {
      return;
    }

    Bmob.Sms.verifySmsCode(newPhone, newCode).then(function (obj) {
      console.log("msg:" + obj.msg); //
      app.turnBack();
    }, function (err) {
      console.log(err);
    });
  },
  inputOldCode: function (e) {
    this.setData({
      oldCode: e.detail.value
    })
  },



  nextStep: function () {
    var that = this;
    if (!this.data.oldCode) {
      app.showModal({
        content: '请输入验证码'
      })
      return;
    }
    if (this.data.nextStepDisabled) {
      return;
    }

    this.setData({
      nextStepDisabled: true
    })
    app.sendRequest({
      url: '/index.php?r=AppData/VerifyPhone',
      method: 'post',
      data: {
        code: this.data.oldCode
      },
      success: function () {
        that.setData({
          hideVerifyPhone: true,
          hideBindNewPhone: false
        })
      },
      complete: function () {
        that.setData({
          nextStepDisabled: false
        })
      }
    })
  },
  getPhoneNumber: function (e) {
    app.sendRequest({
      hideLoading: true,
      url: '/index.php?r=AppUser/GetPhoneNumber',
      data: {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      },
      success: function (res) {
        app.setUserInfoStorage({
          phone: res.data
        })
        app.showToast({
          title: '授权成功',
          icon: 'success',
          success: function(){
            setTimeout(function () {
              app.turnBack();
            }, 800);
          }
        })
      }
    })
  }

})
